package org.example;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;

class Solution {
    public int halveArray(int[] nums) {
        double sum = 0;
        PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2) -> o2.compareTo(o1));
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            queue.offer(nums[i]);
        }
        double target = sum / 2;
        int count = 0;
        while (sum > target) {
            int peek = queue.poll() / 2;
            sum -= peek * 2;
            sum += peek;
            queue.offer(peek);
            count++;
        }
        return count;
    }
}